package com.onepower.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onepower.core.domain.request.SysUserForm;
import com.onepower.core.domain.response.SysUserInfo;
import com.onepower.core.modules.repository.entity.SysUser;
import com.onepower.core.modules.service.IMatchRefereeService;
import com.onepower.core.modules.service.ISysUserService;
import com.onepower.core.result.CommonResult;
import com.onepower.core.utils.ResultUtils;
import com.onepower.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.time.Duration;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author kin
 * @since 2018-08-01
 */
@RestController
@RequestMapping("/api/sys/user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private RedisTemplate<Serializable, Object> redisTemplate;
    
    @Autowired
    private IMatchRefereeService matchRefereeService;

    @Autowired
    HttpServletRequest request;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "20") int pageSize) {
        Page<SysUserInfo> userList = sysUserService.getUserList(new Page<>(pageNum, pageSize));
        return ResultUtils.ok(userList);
    }

    @RequestMapping(value = "info/{userId}", method = RequestMethod.GET)
    public CommonResult info(@PathVariable("userId") Integer userId) {
        return ResultUtils.ok(sysUserService.getUser(userId));
    }

    @RequestMapping(value = "current/info", method = RequestMethod.GET)
    public CommonResult currentUserInfo() {
        return ResultUtils.ok(sysUserService.getUser(SecurityUtils.getCurrentUserId(request)));
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public CommonResult save(@RequestBody SysUserForm user) {
        sysUserService.saveUser(user);
        return ResultUtils.ok();
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public CommonResult update(@RequestBody SysUserForm user) {
        sysUserService.saveUser(user);
        return ResultUtils.ok();
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public CommonResult delete(@RequestBody Integer[] userIds) {
        sysUserService.deleteUser(Arrays.asList(userIds));
        return ResultUtils.ok();
    }

    @RequestMapping(value = "resetPassword", method = RequestMethod.POST)
    public CommonResult resetPassword(@RequestBody Map<String,Object> map) {
        sysUserService.restPassword((int)map.getOrDefault("userId","0"), map.getOrDefault("password","").toString());
        return ResultUtils.ok();
    }

    @RequestMapping(value = "queryLoginStates", method = RequestMethod.POST)
    public CommonResult queryLoginStates(@RequestParam int refereeId) {
        String id = (String) redisTemplate.opsForValue().get("resetLogin"+refereeId);
        //更改在线时间
        matchRefereeService.updateCheckTime(refereeId);
        return ResultUtils.ok(id);
    }

    @RequestMapping(value = "deleteLoginStates", method = RequestMethod.POST)
    public CommonResult deleteLoginStates(@RequestParam int refereeId) {
        redisTemplate.delete("resetLogin"+refereeId);
        return ResultUtils.ok();
    }

    @RequestMapping(value = "queryUserByPassword", method = RequestMethod.POST)
    public CommonResult queryUserByPassword(@RequestParam(value = "userId", required = true) int userId,
                                            @RequestParam(value = "password", required = true) String password
                                            ){
        SysUser sysUser = sysUserService.queryUserByPassword(userId,password);
        if (sysUser != null){
            return  ResultUtils.ok(true);
        }else {
            return  ResultUtils.ok(false);
        }
    }

}
